package com.orifine.sql.simple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

public class SimpleSort {
	private Map<String, String> sort;
	private ArrayList<Map<String, String>> sortList;
	
	public SimpleSort(Map<String, String> sorts){
		{
			sort = new TreeMap<String, String>();
			sortList = new ArrayList<Map<String,String>>();
			Iterator<String> itor = sorts.keySet().iterator();
			while(itor.hasNext()){
				String key = itor.next();
				addSort(key, sorts.get(key));
			}
		}
	}
	
	public SimpleSort(){
		sort = new TreeMap<String, String>();
		sortList = new ArrayList<Map<String,String>>();
	}
	
	public SimpleSort filterSort(String... keys){
		Map<String, String> sorts = new TreeMap<String, String>();
		for(int i = 0; i < keys.length; i++){
			String value = sort.get(keys[i]);
			if(value != null){
				addSort(keys[i], value);
			}
		}
		sort = sorts;
		return this;
	}
	
	public SimpleSort addSort(String key, String value){
		if(value == null) return this;
		if(value.toUpperCase().equals("DESC") || value.toUpperCase().equals("ASC")){
			sort.put(key, value.toUpperCase());
			Map<String, String> entry = new HashMap<String, String>();
			entry.put("key", key);
			entry.put("value", value);
			sortList.add(entry);
		}
		return this;
	}
	
	public ArrayList<Map<String, String>> getEntry(){
		return sortList;
	}
}
